﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;
using DataAccess.BusinessObject;
using DataAccess.DataObject;

namespace EnmineKaitlin.Common
{
    public partial class DropDowListCategory : System.Web.UI.UserControl
    {
        ListItem li;
        BCategory _BCategory = new BCategory();
        string _LangID="vn";

        public string LangID
        {
            get { return _LangID; }
            set { _LangID = value; }
        }
        
        public Unit Width
        {
            get { return ddlCategory.Width; }
            set { ddlCategory.Width = value; }
        }
        public bool Enable
        {
            get { return ddlCategory.Enabled; }
            set { ddlCategory.Enabled = value; }
        }
        public string SelectedValue
        {
            get { return ddlCategory.SelectedValue; }
            set { ddlCategory.SelectedValue = value; }
        }        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataBind();                
            }
        }
        public override void DataBind()
        {
            ddlCategory.Items.Clear();
            li = new ListItem("--- Không chọn ---", "0");
            ddlCategory.Items.Add(li);
            IList<OCategory> lstCategory = _BCategory.Get(0, 0,_LangID);
            foreach (OCategory objCategory in lstCategory)
            {
                li = new ListItem(objCategory.CatName, objCategory.CatID.ToString());
                ddlCategory.Items.Add(li);
                additemchild(ddlCategory, objCategory.CatID, li, 0);
            }
            ddlCategory.SelectedValue = SelectedValue;
            ddlCategory.DataBind();
        }
        public void additemchild(DropDownList ddl, int CatParent, ListItem li,int level)
        {
            IList<OCategory> lstCategoryChild = _BCategory.Get(0, CatParent,_LangID);
            if (lstCategoryChild.Count > 0)
            {
                level += 1;
                foreach (OCategory objCategoryChild in lstCategoryChild)
                {
                    string html = "";
                    for (int i = 0; i < level*3; i++)
                    {
                        html += "&nbsp;";
                    }
                    li = new ListItem(Server.HtmlDecode(html + objCategoryChild.CatName), objCategoryChild.CatID.ToString());
                    ddl.Items.Add(li);                    
                    additemchild(ddl, objCategoryChild.CatID, li,level);
                }
            }
        }
    }
}